<template>
  <view class="auth-box"></view>
</template>

<script>
  import qs from '@/utils/qs.js'
  import {platformMap}  from '@/common/models.js'
  import mixin from '@/mixins/auth.js';
  
  export default {
    mixins: [mixin],
    data() {
      return {
        reURL: '',
        vid: 0,
        mid: 0,
        loginState: '',
        loginCode: ''
      }
    },
    onLoad(e) {
      if (e.reurl) {
        this.reURL = e.reurl
      }
      if (e.vid) {
        this.vid = e.vid
        this.mid = e.mid
      }
      uni.showLoading({
        title: this.$t('login.authing') // todo
      })
        
      if (e.code && e.state) {
        this.loginState = e.state
        this.loginCode = e.code
        this.fbLoginByCode()
      }
    },
    methods: {
      fbLoginByCode () {
        const that = this
        const paramsObj = qs.parse(that.loginState);
        let locationUrl = this.configs.webUrl + '/auth/facebook';
        // let locationUrl = 'https://localhost:9999/m/auth/facebook';
        // 从state取出vid，mid
        let requestData = {
          code: that.loginCode,
          url: locationUrl
        }
          
        if (paramsObj.vid) {
          requestData.vid = paramsObj.vid
          that.vid = paramsObj.vid
        }
        if (paramsObj.mid) {
          that.mid = paramsObj.mid
        }
        
        if (uni.getStorageSync('userFrom')) {
          requestData.source = uni.getStorageSync('userFrom')
        }
        if (uni.getStorageSync('guestId')) {
          requestData['guest_id'] = uni.getStorageSync('guestId')
        }
        uni.request({
          url: this.configs.apiUrl + '/api/user/facebook',
          data: requestData,
          success(res) {
            that.handleLoginSuccess(res, 'facebook');
          }
        })
      },
    }
  }
</script>

<style>
  .auth-box {
    display: flex;
    align-items: center;
    height: 100vh;
    width: 100vw;
    justify-content: center;
  }
  .button-login {
    width: 654rpx;
    height: 98rpx;
    box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.2);
    background-size: contain;
  }
  .button-login-fb {
    background-image: url(../static/img/login/login_fb.png);
  }
</style>